package com.buydeem.composite;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zengchao
 * @date 2022-05-08 11:56:57
 */
public class Composite extends Component{

    private List<Component> components = new ArrayList<>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        components.add(component);
    }

    @Override
    public void remove(Component component) {
        components.add(component);
    }

    @Override
    public Integer getSalary() {
        int sum = 0;
        for (Component component : components) {
            sum = sum + component.getSalary();
        }
        return sum;
    }

    @Override
    public void print(Integer level) {
        for (int i = 0; i < level; i++) {
            System.out.print("-");
        }
        System.out.println(name);

        components.forEach(component -> component.print(level+1));
    }
}
